package com.megan.design.pattern.creational.simplefactory;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author: megan
 * @Data: 2018/8/24 上午8:08
 * @Description:
 */
@Slf4j
public class VideoFactory {
    public static final String JAVA = "java";
    public static final String PYTHON = "python";

    public Video getVideo(String type) {
        if (JAVA.equalsIgnoreCase(type)) {
            return new JavaVideo();
        } else if (PYTHON.equalsIgnoreCase(type)) {
            return new PythonVideo();
        }
        return null;
    }

    /**
     * 使用反射优化工厂类
     * @param c
     * @return
     */
    public Video getVideo(Class c){
        Video video = null;
        try {
            video = (Video) Class.forName(c.getName()).newInstance();
        } catch (InstantiationException e) {
            log.error("InstantiationException-->{}",e);
        } catch (IllegalAccessException e) {
            log.error("IllegalAccessException-->{}",e);
        } catch (ClassNotFoundException e) {
            log.error("ClassNotFoundException-->{}",e);
        }
        return video;
    }
}
